﻿using System;
using System.Collections.Generic;
using Salient.ServiceModel;
using WcfServiceShared;

namespace WcfServiceChannelClient
{
    /// <summary>
    /// Programatically Granting a Namespace Reservation 
    /// http://msdn.microsoft.com/en-us/magazine/cc163531.aspx
    /// 
    /// http://nimtug.org/blogs/damien-mcgivern/archive/2009/05/26/wcf-communicationobjectfaultedexception-quot-cannot-be-used-for-communication-because-it-is-in-the-faulted-state-quot-messagesecurityexception-quot-an-error-occurred-when-verifying-security-for-the-message-quot.aspx
    /// </summary>
    internal class Program
    {
        private static void Main(string[] args)
        {


            Console.WriteLine("---------------------------------------------------------------------");
            Console.WriteLine("Please wait for the service to become ready and then press enter to");
            Console.WriteLine("initiate request.\r\n");
            Console.ReadLine();

            ServiceInvoker.UsingFactory<IService>(channel =>
                {
                    // This should work just fine, as its a standard WCF service call that
                    // works just fine, beacuse we have adoned the WCF service interface
                    // with the ReferencePreservingDataContractFormatAttribute to 
                    // allow circular referencesk

                    List<Person> people = channel.GetPeople();

                    foreach (Person person in people)
                    {
                        Console.WriteLine(
                            string.Format(
                            "Obtained person {0}, who has {1} Children", 
                            person.Name, person.Children.Count));
                    }

                    // We expect this to fail as this the proxy method call,
                    // is calling a WCF service method that has the default DataContractSerializer
                    // which CAN'T deal with circular references

                    try
                    {

                        // if you decide to use an Http binding, just don't do this, it ties the serializer up
                        // in a knot and locks the app
                        channel.GetPeopleWithOutCicularReferencing();

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(String.Format("{0}: {1}", ex.GetType(),
                                                        ex.Message));
                    }
                });

            Console.ReadLine();
        }
    }




}




